package com.pub.cloud.system.status.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import com.pub.cloud.system.status.config.LinuxOsParmConfig;
import org.springframework.beans.factory.annotation.Autowired;

import com.pub.cloud.system.status.config.LinuxOsParmConfig;

/**
 * 取得linux系统下的cpu、内存信息
 *
 * <p>
 * @author javer QQ:84831612
 * </p>
 * 
 * @date 2005
 */
public final class LinuxSystemTool {
	/**
	 * get memory by used info
	 *
	 * @return int[] result
	 *         result.length==4;int[0]=MemTotal;int[1]=MemFree;int[2]=SwapTotal;int[3]=SwapFree;
	 * @throws IOException
	 * @throws InterruptedException
	 */


	
	public static int[] getMemInfo() throws IOException, InterruptedException {
		File file = new File("/proc/meminfo");
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
		int[] result = new int[4];
		String str = null;
		StringTokenizer token = null;
		while ((str = br.readLine()) != null) {
			token = new StringTokenizer(str);
			if (!token.hasMoreTokens())
				continue;

			str = token.nextToken();
			if (!token.hasMoreTokens())
				continue;

			if (str.equalsIgnoreCase("MemTotal:"))
				result[0] = Integer.parseInt(token.nextToken());
			else if (str.equalsIgnoreCase("MemFree:"))
				result[1] = Integer.parseInt(token.nextToken());
			else if (str.equalsIgnoreCase("SwapTotal:"))
				result[2] = Integer.parseInt(token.nextToken());
			else if (str.equalsIgnoreCase("SwapFree:"))
				result[3] = Integer.parseInt(token.nextToken());
		}

		return result;
	}

	/**
	 * get memory by used info
	 *
	 * @return float efficiency
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public static float getCpuInfo() throws IOException, InterruptedException {
		File file = new File("/proc/stat");
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
		StringTokenizer token = new StringTokenizer(br.readLine());
		token.nextToken();
		int user1 = Integer.parseInt(token.nextToken());
		int nice1 = Integer.parseInt(token.nextToken());
		int sys1 = Integer.parseInt(token.nextToken());
		int idle1 = Integer.parseInt(token.nextToken());

		Thread.sleep(1000);

		br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
		token = new StringTokenizer(br.readLine());
		token.nextToken();
		int user2 = Integer.parseInt(token.nextToken());
		int nice2 = Integer.parseInt(token.nextToken());
		int sys2 = Integer.parseInt(token.nextToken());
		int idle2 = Integer.parseInt(token.nextToken());

		return (float) ((user2 + sys2 + nice2) - (user1 + sys1 + nice1))
				/ (float) ((user2 + nice2 + sys2 + idle2) - (user1 + nice1 + sys1 + idle1));
	}

	public static Map<String, Integer> getMemInfoExp() {

		Map<String, Integer> result = new HashMap<String, Integer>();
		result.put("status", 200);
		result.put("memTotal", 0);
		result.put("memFree", 0);
		result.put("swapTotal", 0);
		result.put("swapFree", 0);

		try {
			int[] memInfo = LinuxSystemTool.getMemInfo();
			result.put("memTotal", memInfo[0]);
			result.put("memFree", memInfo[1]);
			result.put("swapTotal", memInfo[2]);
			result.put("swapFree", memInfo[3]);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("status", 500);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("status", 501);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("status", 502);
		}

		/*
		 * System.out.println("MemTotal：" + memInfo[0]); System.out.println("MemFree：" +
		 * memInfo[1]); System.out.println("SwapTotal：" + memInfo[2]);
		 * System.out.println("SwapFree：" + memInfo[3]);
		 */

		return result;
	}

	public static Map<String, Object> getDiskInfoExp() {

		Map<String, Object> result = new HashMap<String, Object>();
		result.put("status", 200);

		try {
			// 磁盘空间：
			Process pro = null;
			Runtime r = Runtime.getRuntime();
			String command = "df -h";
			pro = r.exec(command);
			BufferedReader in = new BufferedReader(new InputStreamReader(pro.getInputStream()));
			// String line = null;
			System.out.println("目前所有文件系统的可用空间及使用情形 Filesystem Size Used Avail Use% Mounted on");
			int i = 0;
			String line=in.readLine();
			while ((line=in.readLine()) != null) {
				String[] Info = line.split("\\s+");
				System.out.println(Info[0]);
				System.out.println(Info[1]);
				System.out.println(Info[2]);
				System.out.println(Info[3]);
				result.put(String.valueOf(i), Info);
				i++;
			}
			in.close();
			pro.destroy();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;

	}
	
	
	public static Map<String, Object> getDiskAlertExp(int Threshold) {

		Map<String, Object> result = new HashMap<String, Object>();
//		result.put("status", 200);
		try {
			// 磁盘空间：
			Process pro = null;
			Runtime r = Runtime.getRuntime();
			String command = "df -h";
			pro = r.exec(command);
			BufferedReader in = new BufferedReader(new InputStreamReader(pro.getInputStream()));
			// String line = null;
			System.out.println("目前所有文件系统的空间使用情形及告警情况");
			int i = 0;
			String line=in.readLine();
			while ((line =in.readLine()) != null) {
				try {
					String[] Info=line.split("\\s+");
					System.out.println(line);
					String a =Info[4];
					String[] aa=a.split("\\%");
					System.out.println("磁盘使用率："+aa[0]);
					Map<String, Object> useinfo = new HashMap<String, Object>();
					if((Integer.parseInt(aa[0]))>=Threshold) {
						a="当前磁盘使用空间超过"+Threshold+"%,目前使用空间为"+Info[4]+",请注意清理磁盘。";
						useinfo.put("status", 1);
						useinfo.put("info", a);
					}else {
						a="当前磁盘使用空间未超过"+Threshold+"%,目前使用空间为"+Info[4]+"。";
						useinfo.put("status", 0);
						useinfo.put("info", a);
					}
					System.out.println(a);
//					result.put(Info[5], Info[4]);
//					result.put(Info[5], a);
					result.put(Info[5], useinfo);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

//				i++;
			}
			in.close();
			pro.destroy();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;

	}
	
	public static void main(String[] args) throws Exception {
		int[] memInfo = LinuxSystemTool.getMemInfo();
		System.out.println("MemTotal：" + memInfo[0]);
		System.out.println("MemFree：" + memInfo[1]);
		System.out.println("SwapTotal：" + memInfo[2]);
		System.out.println("SwapFree：" + memInfo[3]);

		System.out.println("CPU利用率：" + LinuxSystemTool.getCpuInfo());
	}

}